﻿


using IotClientService.Managers;
using IotContract.Interfaces;
using Microsoft.Extensions.DependencyInjection;

using ProtoBuf.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IotClientService.Operations.plc;
using IotClientService.Operations.bcr;
using IotClientService.Operations.hk;
using IotContract.Dtos.Clients.Equips;

using XT.Grpc.Services;
using IotClientService.Grpcs;
using IotContract.Interfaces.Process;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Reflection.Metadata;
using XT.Common;
using XT.Common.Services;
using IotContract.Interfaces.Http;
using IotClientService.Services;
using IotClientService.Handles.lgs;
using IotContract.Enums;

namespace IotClientService;
public static class Register
{
    /// <summary>
    /// 注册服务
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection RegisterClientService(this IServiceCollection services,bool isIotClient=true)
    {
        // 添加http请求
        services.AddOriginHttpClient();
        // 添加LgApi请求服务
        services.AddTransient<ILgApiService, LgCallApiService>();
        //services.AddTransient<ILgApiService, LgMockApiService>(); 
        // 注入signalr服务
        services.AddSignalRService();
        // 设备添加日志
        services.AddSingleton<ILogService, LogService>();

        services.AddSingleton<IClientWebService, ClientWebService>();

        // 作为iot程序，才启动设备采集服务
        if (isIotClient)
        {
            // 设备manager
            var handles = typeof(SiemensS7Manager).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && x.GetInterface(nameof(IEquipManager)) != null);
            foreach (var handle in handles)
            {
                services.AddSingleton(typeof(IEquipManager), handle);
            }
            // 设备操作
            var operations = typeof(SiemensS7Manager).Assembly.GetTypes().Where(x => x.IsClass && !x.IsAbstract && x.GetInterfaces().FirstOrDefault(i => i.Name.Contains("IEquipOperation")) != null);

            foreach (var opera in operations)
            {
                var inter = opera.GetInterfaces().FirstOrDefault();
                if (opera != null)
                {
                    services.AddTransient(inter, opera);
                }
            }
            services.AddSingleton<IClientMainManager, ClientMainManager>();

            //services.AddKeyedTransient<SimulatorHandleBase, SimulatorHandleSocket>(IotEquipType.Simulator.ToString());
            // 注册ocv
            services.AddKeyedTransient<LgHandleBase, LgHandleOcv>(IotEquipType.OCV.ToString());
            // 注册fmt
            services.AddKeyedTransient<LgHandleBase, LgHandleFmt>(IotEquipType.FMT.ToString());
            // DCIR
            services.AddKeyedTransient<LgHandleBase, LgHandleDCIR>(IotEquipType.DCIR.ToString());
            // 筛选机
            services.AddKeyedTransient<LgHandleBase, LgHandleSel>(IotEquipType.SEL.ToString());
            // 组盘机
            services.AddKeyedTransient<LgHandleBase, LgHandleAssemble>(IotEquipType.AEB.ToString());
            // 拆盘机
            services.AddKeyedTransient<LgHandleBase, LgHandleDisAssemble>(IotEquipType.DAEB.ToString());
            // 注液机
            services.AddKeyedTransient<LgHandleBase, LgHandleLiquid>(IotEquipType.LIQUID.ToString());
            // 包膜机
            services.AddKeyedTransient<LgHandleBase, LgHandleLaminate>(IotEquipType.LAMINAT.ToString());
            // 打钉机
            services.AddKeyedTransient<LgHandleBase, LgHandleNailing>(IotEquipType.NAIL.ToString());
            // 清洗机
            services.AddKeyedTransient<LgHandleBase, LgHandleClear>(IotEquipType.CLEAR.ToString());
            // 后氦检机
            services.AddKeyedTransient<LgHandleBase, LgHandleHelium>(IotEquipType.HELIUM.ToString());
            // 密封打钉机
            services.AddKeyedTransient<LgHandleBase, LgHandleSealNail>(IotEquipType.SEAL.ToString());

            // 外观检测机机
            services.AddKeyedTransient<LgHandleBase, LgHandleInspect>(IotEquipType.INSPECT.ToString());


            // 火灾上位机
            services.AddKeyedTransient<LgHandleBase, LgHandleFire>(IotEquipType.FIRE.ToString());
            // 预充
            services.AddKeyedTransient<LgHandleBase, LgHandlePreCharge>(IotEquipType.PRECHARGE.ToString());
            // 分容补电
            services.AddKeyedTransient<LgHandleBase, LgHandleCapacity>(IotEquipType.CAPACITY.ToString());
        }





        return services;
    }
    /// <summary>
    /// 注册grpc客户端服务
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection RegisterClientGrpc(this IServiceCollection services)
    {
        services.AddSingleton<GrpcChannelService>();
        services.AddSingleton<IConnectDataService, ClientConnectDataService>();
        return services;
    }
}
